package cn.pzx.base_module.base

import android.app.ProgressDialog
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import cn.pzx.base_module.R
import cn.pzx.base_module.event.EventMessage
import cn.pzx.base_module.http.ErrorResult
import com.blankj.utilcode.util.ToastUtils
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.lang.reflect.ParameterizedType

/**
 * @author pzx
 * @description:
 * @date :3/8/21 10:58 AM
 */
abstract class BaseActivity<VM : BaseViewModel, VB : ViewBinding> : AppCompatActivity() {

    lateinit var mContext: FragmentActivity
    lateinit var vm: VM
    lateinit var vb: VB

    private var loadingDialog: ProgressDialog? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        //注意 type.actualTypeArguments[0]=BaseViewModel，type.actualTypeArguments[1]=ViewBinding
        val type = javaClass.genericSuperclass as ParameterizedType
        val clazz1 = type.actualTypeArguments[0] as Class<VM>
        vm = ViewModelProvider(this).get(clazz1)

        val clazz2 = type.actualTypeArguments[1] as Class<VB>
        val method = clazz2.getMethod("inflate", LayoutInflater::class.java)
        vb = method.invoke(null, layoutInflater) as VB

        setContentView(vb.root)

        mContext = this

        init()
        initView()
        initClick()
        initData()
        initVM()

    }

    abstract fun initView()

    abstract fun initClick()

    abstract fun initData()

    abstract fun initVM()

    fun initTitleBar(titleBar: CommonTitleBar) {
        titleBar.setBackgroundResource(R.drawable.shape_bg)
        titleBar.leftCustomView?.setOnClickListener {
            onBackPressed()
        }

    }

    private fun init() {

        //loading
        vm.isShowLoading.observe(this, Observer {
            if (it) showLoading() else dismissLoading()
        })
        //错误信息
        vm.errorData.observe(this, Observer {
            if (it.show) ToastUtils.showShort(it.errMsg)
            errorResult(it)
        })
//        IPermissionInterceptor


        if (getIsRegisterBus()) EventBus.getDefault().register(this)
    }

    fun getIsRegisterBus(): Boolean {
        return false
    }


    //事件传递
    @Subscribe
    fun onEventMainThread(msg: EventMessage) {
        receivedEvent(msg)
    }

    fun showLoading() {
        if (loadingDialog == null) {
            loadingDialog = ProgressDialog(this)
        }
        loadingDialog!!.show()
    }

    fun dismissLoading() {
        loadingDialog?.dismiss()
        loadingDialog = null
    }

    /**
     * 消息、事件接收回调
     */
    open fun receivedEvent(msg: EventMessage) {
    }

    /**
     * 接口请求错误回调
     */
    open fun errorResult(errorResult: ErrorResult) {}


    /**
     * 销毁时取消EventBus监听
     */
    override fun onDestroy() {
        super.onDestroy()
        if (getIsRegisterBus()) EventBus.getDefault().unregister(this)
    }


}